package com.cungmua.app.utils;

import android.util.Log;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * Created by kelvin.nguyen on 6/16/14.
 */
public class JsonUtils {
    private static final String TAG = "error";

    // this helper method can be used to make JSON parsing a one-line operation
    public static <T> T parseJson(JsonNode node, Class<T> klass) {
        try {
            Log.e(TAG,JsonUtils.defaultMapper().treeToValue(node, klass).toString());
            return JsonUtils.defaultMapper().treeToValue(node, klass);
        } catch (IOException e) {
            Log.e(TAG, "Failed to parse JSON entity " + klass.getSimpleName(), e);
            throw new RuntimeException(e);
        }
    }

    // re-use a single ObjectMapper so we're not creating multiple object mappers
    private static ObjectMapper sObjectMapper = new ObjectMapper();
    public static ObjectMapper defaultMapper() {
        return sObjectMapper;
    }
}
